
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>

#include<thread>
#include<chrono>

#include<pthread.h>

using namespace std;

#define BUFF_LINE 512
#define BUFF_LINE_2 1024

bool tmp_false = false;
bool tmp_true = true;

/***************************************
			Class Timer
***************************************/
class Timer
{
public:
	Timer();
	Timer( chrono::seconds time, bool **is_running);
	~Timer();

	void scheduleRunningTime();	
//private:
	chrono::seconds t;
	bool **is_running = NULL;
};

Timer::Timer()
{
	this->t = chrono::seconds(0);
	this->is_running = NULL;
}

Timer::Timer( chrono::seconds time, bool **is_running )
{
	this->t = time;
	this->is_running = is_running;
}

Timer::~Timer()
{
}

void
Timer::scheduleRunningTime()
{
	printf("start sleep...\n");
	this_thread::sleep_for( t );	
	*is_running = &tmp_false;
	printf("stop sleep, and set *is_running = &tmp_false...\n");
}

class TimerThread
{
public:
	TimerThread( chrono::seconds(time), bool **is_running);
	TimerThread();
	~TimerThread();
	void run_independent_timer();
	void wait_for_timer();
private:
	Timer timer;
	thread *t_timer;

};

TimerThread::TimerThread( chrono::seconds(time), bool** is_running )
{
	timer = Timer( time, is_running);
}

TimerThread::TimerThread()
{
	timer = Timer();
}


TimerThread::~TimerThread()
{
	if(t_timer != NULL)
	{
		delete(t_timer);	
		t_timer=NULL;
	}
}

void
TimerThread::run_independent_timer()
{
	t_timer = new thread(&Timer::scheduleRunningTime, &timer );
	//t_timer->detach();
}
void
TimerThread::wait_for_timer()
{
	t_timer->join();
}



/***************************************
		   Function Main	
***************************************/


int main(int argc, char **argv)
{
	bool *ptr = &tmp_true;

	TimerThread timeThread_sender(chrono::seconds(10), &ptr);

	timeThread_sender.run_independent_timer();

	printf("start...\n");
	while(1)
	{
		if(*ptr == false)
		{
			break;	
		}	
	}
	printf("stop...\n");

	timeThread_sender.wait_for_timer();

	//return 0;
}

